<template>
    <a-modal v-model="filterVisible" title="格兰杰因果检验" :footer="null" :centered="true">
        <a-spin :spinning="spinning" tip="计算中……">
            <a-row type="flex" :gutter="[16,16]">
                <a-col flex="100px"><div class="label">结果变量：</div></a-col>
                <a-col flex="auto">
                    <a-select style="width:100%" v-model="resultIndex">
                        <a-select-option v-for="item in list" :key="item.index" :value="item.index">{{item.name}}</a-select-option>
                    </a-select>
                </a-col>
            </a-row>
            <a-row type="flex" :gutter="[16,16]">
                <a-col flex="100px"><div class="label">原因变量：</div></a-col>
                <a-col flex="auto">
                    <a-select style="width:100%" v-model="reasonIndex">
                        <a-select-option v-for="item in list" :key="item.index" :value="item.index">{{item.name}}</a-select-option>
                    </a-select>
                </a-col>
            </a-row>
            <a-row type="flex" :gutter="[16,16]">
                <a-col flex="100px"><div class="label">滞后阶数：</div></a-col>
                <a-col flex="auto">
                    <a-input-number v-model="lag" :min="0" />
                </a-col>
            </a-row>
            <div class="btns">
                <a-button type="primary" ghost @click="cancel">取消</a-button>
                <a-button type="primary" @click="goApply">应用</a-button>
            </div>
        </a-spin>
    </a-modal>
</template>

<script>
import { messageTips, setSessionItem } from '../../../until/tools';
export default {
    props:['visible','sheetId'],
    data() {
        return {
            filterVisible:false,
            lag:5,
            list:[],
            resultIndex:null,
            reasonIndex:null,
            spinning:false
        }
    },
    watch:{
        visible(value) {
            if(value) {
                this.resultIndex = null;
                this.reasonIndex = null;
                this.lag = 5;
                this.spinning = false;
                this.$http.cloud_get_variables({action:'regression',sheetId:this.sheetId}).then(res=>{
                    if(res.deptVars) {
                        this.list = res.deptVars;
                    }
                })
                this.filterVisible = true;
            }
        }
    },
    methods:{
        cancel() {
            this.filterVisible = false;
        },

        goApply() {
            if(this.resultIndex===null) {
                messageTips('请选择结果变量',2);
                return false;
            }
            if(this.reasonIndex===null) {
                messageTips('请选择原因变量',2);
                return false;
            }
            let params = {
                reasonIndex:this.reasonIndex,
                resultIndex:this.resultIndex,
                lags:this.lag,
                sheetId:this.sheetId
            }
            this.spinning = true;
            this.$http.cloud_set_granger(params).then(res=>{
                this.spinning = false;
                if(!res.success) {
                    messageTips(res.message,1);
                    return;
                }
                this.filterVisible = false;
                let result = {'analysis_grangerCheck':res}
                setSessionItem('analysis_grangerCheck',JSON.stringify(result));
                let {href} = this.$router.resolve({path: '/analysis_grangerCheck'});
                window.open(href, '_blank');
            });
        },
    }
}
</script>

<style lang="scss" scoped>
    .label {
        font-weight: bold;
        height:32px;
        line-height: 32px;
    }
</style>